// pages/addyuyue/index.js

const app = getApp();
var dateTimePicker = require('../../utils/dateTimePicker.js');
const date = new Date();
const years = [];
const months = [];
const days = [];
const hours = [];
const minutes = [];
//获取年
for (let i = 2022; i <= date.getFullYear() + 5; i++) {
  years.push("" + i);
}
//获取月份
for (let i = 1; i <= 12; i++) {
  if (i < 10) {
    i = "0" + i;
  }
  months.push("" + i);
}
//获取日期
for (let i = 1; i <= 31; i++) {
  if (i < 10) {
    i = "0" + i;
  }
  days.push("" + i);
}
//获取小时
for (let i = 0; i < 24; i++) {
  if (i < 10) {
    i = "0" + i;
  }
  hours.push("" + i);
}
//获取分钟
for (let i = 0; i < 60; i++) {
  if (i < 10) {
    i = "0" + i;
  }
  minutes.push("" + i);
}
Page({

  /**
   * 页面的初始数据
   */
  
  data: {
    sid: '',
    lid: '',
    dname:'', //美发店
    fname:'',//理发师
    array: ['男', '女'],
    index: 0,
    // xingbie: '男',

    datetimeArray:[[],[]],
    times:['9:00-10:00','10:00-11:00','11:00-12:00','13:00-14:00','14:00-15:00','15:00-16:00','16:00-17:00',
             '17:00-18:00','18:00-19:00','20:00-21:00','21:00-22:00'],
    multiIndex:[0,0],
    chooseTime:"",
    yuytimeList:[{lid:4,riqi:"2022-04-25 11:00-12:00"},{lid:4,riqi:"2022-04-25 10:00-11:00"},
                 {lid:4,riqi:"2022-04-26 11:00-12:00"},{lid:4,riqi:"2022-04-25 9:00-10:00"},
                 {lid:3,riqi:"2022-04-26 11:00-12:00"},{lid:2,riqi:"2022-04-25 11:00-12:00"}],
    
    // timestamp:'',//时间戳
    feiyong: 0 ,  //预约费
    lfdappointlist:[],//全部预约订单
    fList:[],//已有订单
  },

 
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
    var datas = this.getDates(7)
    // this.getappoint()
    this.data.sid = options.sid;
    this.data.lid = options.lid;
    this.data.dname = options.dname;
    this.data.fname = options.name;
    this.data.feiyong = options.feiyong;
    console.log("sid:"+options.sid+ "lid:" +options.lid+ '美发店dname:' +options.dname+ 'name:'+options.name+' feiyong:'+options.feiyong)
    var dname = options.dname
    var fname = options.name
    var feiyong = options.feiyong
   
    this.setData({
      datetimeArray:[datas,this.data.times],
      dname: dname,
      fname :fname,
      feiyong:feiyong,
    });
    var fList = this.getTrueTime()
    let chooseTime = this.data.datetimeArray[0][0]+" "+this.data.datetimeArray[1][0]
    this.setData({
      fList:fList,
      chooseTime:chooseTime, 
    })
    
  },

  // 提交预约
  formSubmit: function (e) {
    var that = this;
    var form = e.detail.value;
    wx.request({
      url: app.globalData.headurl + '/Lfdappoint/addLfdappoint',
      data: {
        xingming: form.name,
        xingbie: form.xingbie,
        dianhua: form.tel,
        riqi: that.data.chooseTime,
        beizhu: form.remark,
        sid: that.data.sid,
        lid: that.data.lid,
        // dname:that.data.dname,
        yuefen:that.data.fname,
        status: 0,
        openid: app.globalData.openid,
        money:parseInt(that.data.feiyong)
      },
      method: "POST",
      header: {
        "content-type": "application/x-www-form-urlencoded;charset=utf-8",
        //token: app.globalData.token
      },
      success: function (r) {
        wx.showToast({
          title: '预约成功',
        })
        wx.navigateTo({
          url: '../yuyue/index',
        })
      }
    })



  },
  //获取未来几天的数组
  getDates(days){
    let datas =this.data.datetimeArray[0]
    // datas.push('---')
    for(var i = 1;i <= days;i++){
      datas.push(this.getDate(i))
    }
    return datas;
  },
  getDate(day) {
    var dd = new Date()
    dd.setDate(dd.getDate() + day);//获取AddDayCount天后的日期 
    var y = dd.getFullYear()
    var m = dd.getMonth() + 1//获取当前月份的日期 
    m = m < 10 ? '0' + m : m
    var d = dd.getDate()
    d = d < 10 ? '0' + d : d
    return y + "-" + m + "-" + d
  },
  bindMultiPickerChange(e) {
    var arr = e.detail.value;
    var one = this.data.datetimeArray[0][arr[0]];
    var two = this.data.datetimeArray[1][arr[1]];
    var newTime = one + " " + two
		this.setData({
			multiIndex: e.detail.value,
			chooseTime: one + " " + two
    })
    this.isTrueTime(newTime)
  },
  //获取系统中该理发师已有的订单
  getTrueTime:function(){
    // console.log(lfdappointlist)
    var tList = this.data.yuytimeList
    var newfid = this.data.lid;
    // console.log("选择好时间",newTime)
    console.log("全部订单",tList)
    var newarr = []
    for(let i=0;i<tList.length;i++){
      var p = tList[i]
      if(tList[i].lid == newfid){
        newarr.push(p)
      }
    }
    return newarr
    // var times = this.data.times
    // console.log("全部数据段",)
  },
  isTrueTime:function(newTime){
    console.log("选择的时间",newTime)
    var fList = this.data.fList
    console.log(fList)
    for(let i=0;i<fList.length;i++){
      var haveTime = fList[i].riqi
      console.log(haveTime) //已经存在的时间
      if(newTime == haveTime){
        console.log("该时间段已经存在")
        wx.showToast({
          title: '该时间段已预约',
        })
        this.onLoad()
      }
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})